package com.cskaoyan.service.wx_order;

import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.goods.Comment;
import com.cskaoyan.bean.goods.CommentExample;
import com.cskaoyan.bean.goods.Goods;
import com.cskaoyan.bean.goods.GoodsExample;
import com.cskaoyan.bean.mall.order.Order;
import com.cskaoyan.bean.mall.order.OrderExample;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.wx_OrderGoods.OrderGoods;
import com.cskaoyan.bean.wx_OrderGoods.OrderGoodsExample;
import com.cskaoyan.bean.wx_order.WXDetailOrderVO;
import com.cskaoyan.bean.wx_order.WXListOrderVO;
import com.cskaoyan.bean.wx_order.WXOrderCommentBO;
import com.cskaoyan.mapper.goods.CommentMapper;
import com.cskaoyan.mapper.goods.GoodsMapper;
import com.cskaoyan.mapper.mall.OrderMapper;
import com.cskaoyan.mapper.wx_OrderGoods.OrderGoodsMapper;
import com.cskaoyan.service.wx_order_before.utils.OrderStatusUtils;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WXOrderServiceImpl implements WXOrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    CommentMapper commentMapper;

    @Override
    public WXListOrderVO listOrders(Integer showType, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
//        List<WXListOrderVO.ListDTO> orders = orderMapper.selectByExample(new OrderExample());
//        PageInfo<Object> pageInfo = new PageInfo<>();
//        PageDataVo<Order> pageDataVo = new PageDataVo<>();
//        pageDataVo.setLimit(limit);
//        pageDataVo.setPage(page);
//        pageDataVo.setPages(pageInfo.getPages());
//        pageDataVo.setTotal((int) pageInfo.getTotal());
//        pageDataVo.setList(orders);
//        return pageDataVo;
        return null;
    }

    @Override
    public WXDetailOrderVO selectOrderById(Integer orderId) {
        WXDetailOrderVO wxDetailOrderVO = new WXDetailOrderVO();
        List<WXDetailOrderVO.OrderGoodsDTO> goodsList = new ArrayList<>();
        OrderExample orderExample = new OrderExample();
        GoodsExample goodsExample = new GoodsExample();
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderExample.Criteria orderCriteria = orderExample.createCriteria();
        OrderGoodsExample.Criteria orderGoodsCriteria = orderGoodsExample.createCriteria();
        //set goodsList from order
//        orderGoodsExample.createCriteria().andOrderIdEqualTo(orderId);
//        for (OrderGoods orderGoods : orderGoodsMapper.selectByExample(orderGoodsExample)) {
//            goodsExample.createCriteria().andIdEqualTo(orderGoods.getGoodsId());
//            Goods goods = goodsMapper.selectByExample(goodsExample).get(0);
//            goodsList.add();
//        }
        orderGoodsCriteria.andOrderIdEqualTo(orderId);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

        // get orderInfo
        Order order = orderMapper.selectByPrimaryKey(orderId);
        WXDetailOrderVO.OrderInfoDTO.HandleOptionDTO handleOptionDTO = OrderStatusUtils.getHandleOptionByOrderStatus().get(Integer.valueOf(order.getOrderStatus()));
        WXDetailOrderVO.OrderInfoDTO orderInfoDTO = new WXDetailOrderVO.OrderInfoDTO(order.getConsignee(),order.getAddress(),order.getAddTime(),
                order.getOrderSn(),order.getActualPrice(),order.getMobile(),order.getMessage(),OrderStatusUtils.getOrderStatusText().get(Integer.valueOf(order.getOrderStatus())),Integer.valueOf(order.getAftersaleStatus()),
                order.getGoodsPrice(),order.getCouponPrice(),order.getId(),order.getFreightPrice(),handleOptionDTO);
        wxDetailOrderVO.setExpressInfo(new ArrayList<>());
        wxDetailOrderVO.setOrderGoods(orderGoods);
        wxDetailOrderVO.setOrderInfo(orderInfoDTO);
        return wxDetailOrderVO;
    }

    @Override
    public void cancelOrder(Integer orderId) {
        Order order = new Order();
        order.setOrderStatus((short)102);
        order.setId(orderId);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void sendComment(WXOrderCommentBO wxOrderCommentBO) {
        OrderGoods orderGoods = orderGoodsMapper.selectByPrimaryKey(wxOrderCommentBO.getOrderGoodsId());
        Comment comment = new Comment();
        comment.setContent(wxOrderCommentBO.getContent());
        comment.setHasPicture(wxOrderCommentBO.getHasPicture());
        comment.setPicUrls(wxOrderCommentBO.getPicUrls());
        comment.setStar(wxOrderCommentBO.getStar());
        comment.setValueId(orderGoods.getGoodsId());
        User primaryPrincipal = (User) (SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal());
        comment.setUserId(primaryPrincipal.getId());
        commentMapper.insertSelective(comment);

        // 根據orderGoodsId 修改 order order_status    order_goods  comment 字段

        Order order = new Order();
        order.setOrderStatus((short) 403);
        order.setId(orderGoods.getOrderId());
        orderMapper.updateByPrimaryKeySelective(order);

        OrderGoods orderGoods1 = new OrderGoods();
        orderGoods1.setComment(comment.getId());
        orderGoods1.setId(wxOrderCommentBO.getOrderGoodsId());
        orderGoodsMapper.updateByPrimaryKey(orderGoods);

    }
}
